package com.clas.liu.myapplication.model.impl;

import android.os.Handler;
import android.util.Log;

import com.clas.liu.myapplication.model.entity.BaseRoot;
import com.clas.liu.myapplication.model.entity.info.Info;
import com.clas.liu.myapplication.model.entity.info.InfoRoot;
import com.clas.liu.myapplication.model.event.EditEvent;
import com.clas.liu.myapplication.model.i.IEditModel;
import com.clas.liu.myapplication.network.NetworkInterfaces;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by liu on 2019/5/5.
 */

public class EditModel extends BaseModel implements IEditModel {

    public EditModel(Handler handler) {
        super(handler);
    }

    @Override
    public void addInfo(Info info) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                EditEvent event = new EditEvent();
                event.setWhat(EditEvent.ADD_INFO_FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Gson gson = new Gson();
                    BaseRoot root = gson.fromJson(response.body().string(),BaseRoot.class);
                    EditEvent event = new EditEvent();
                    event.setWhat(EditEvent.ADD_INFO_OK);
                    event.setRoot(root);
                    postEvent(event);
                } else
                    onFailure(call, new IOException(response.body().string()));

            }
        };
        Gson gson = new Gson();
        String json = gson.toJson(info);
        Log.i("test", "editInfo: "+json);
        interfaces.addInfo(info,callback);
    }

    @Override
    public void editInfo(Info info) {
        Callback callback = new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                EditEvent event = new EditEvent();
                event.setWhat(EditEvent.EDIT_INFO__FAIL);
                postEvent(event);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Gson gson = new Gson();
                    BaseRoot root = gson.fromJson(response.body().string(),BaseRoot.class);
                    EditEvent event = new EditEvent();
                    event.setWhat(EditEvent.EDIT_INFO_OK);
                    event.setRoot(root);
                    postEvent(event);
                } else
                    onFailure(call, new IOException(response.body().string()));

            }
        };
        interfaces.editInfo(info.getId(),info.getRelease_time(),info.getText(),info.getPic(),info.getKey_word(),callback);
    }

    @Override
    protected NetworkInterfaces getNetworkInterface() {
        return new NetworkInterfaces();
    }
}
